/**
 * User: alesj
 * Date: 2004.8.24
 * Time: 10:36:14
 */

package com.generalynx.ecos.web.interceptor;

import com.generalynx.common.constants.WebConst;
import com.generalynx.common.userdata.IModuleUser;
import com.generalynx.common.web.module.Module;
import com.generalynx.common.web.module.Modules;
import com.generalynx.common.web.module.UserModules;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Builds new module structure when changing module.
 */
public class CurrentModuleInterceptor extends HandlerInterceptorAdapter {

    protected final Log logger = LogFactory.getLog(getClass());

    protected final IModuleUser getUser(HttpServletRequest request) {
        return (IModuleUser)request.getSession(false).getAttribute(WebConst.USER);
    }

    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

        String strModuleId = request.getParameter("module");
        if (strModuleId != null) {
            int moduleId;
            try {
                moduleId = Integer.parseInt(strModuleId);
                Module currentModule = Modules.getModule(moduleId);
                if (currentModule == null) {
                    logger.warn("Attempt to access non-existent module with id = " + moduleId);
                } else {
                    IModuleUser user = getUser(request);
                    if (user != null) {
                        try {
                            UserModules userModules = new UserModules(currentModule, user);
                            request.getSession(false).setAttribute(WebConst.USER_MODULES, userModules);
                        } catch (SecurityException e) {
                            logger.warn(e);
                        }
                    }
                }
            } catch (NumberFormatException e) {
                logger.warn("Attempt to access module with non-parsable id '" + strModuleId + "'.");
            }
        }
        return true;
    }

    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object handler,
                           ModelAndView modelAndView) throws Exception {
        IModuleUser user = getUser(request);
        if (user != null && user.isReloaded()) {
            UserModules userModules = (UserModules)request.getAttribute(WebConst.USER_MODULES);
            Module currentModule = userModules.getCurrentModule();
            UserModules newUserModules = new UserModules(currentModule, user);
            //put in both scopes
            request.setAttribute(WebConst.USER_MODULES, newUserModules); //used in current rendering
            request.getSession(false).setAttribute(WebConst.USER_MODULES, newUserModules);
        }
    }

}
